package phoenix.web.test_objects;

import core.base.MailBase;
import core.config.Config;
import org.openqa.selenium.WebElement;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.web.test_objects.crm.MailTests;
import phoenix.web.test_objects.data_providers.MyAccountDataProviders;

public class MyAccountTests extends TestBaseWebExt
{
	public MyAccountTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public MailTests mailTests = new MailTests(manager);
	public MyAccountDataProviders dataProviders = new MyAccountDataProviders(manager);

	/**
	 * MyAccount check mySettingsTests
	 * @param p_newEmail MyAccount new user email
	 */
	public void mySettingsTests(String p_newEmail)
	{
		this.changePasswordTest();
		this.changeEmailTest(p_newEmail);
	}

	/**
	 * MyAccount check correctly change email
	 */
	public void changeEmailTest(String p_newEmail)
	{
		try
		{
			// Goes to site
			wd().openPage("/account");
			// Get values
			String v_password = manager.testUser().getPassword();
			// Fill up the form
			page().myAccountPage().waitForPageLoad();
			page().myAccountPage().clickEditEmail();
			page().myAccountPage().fillEmailForm(p_newEmail, v_password);
			page().myAccountPage().submitEmail();
			page().myAccountPage().waitForEmailSuccess();

			if (manager.testModel().getBuild().equals(Config.BUILD_REL))
			{
				log().info("Test mailbox has been skipped for rel");
				return;
			}

			if (!log().isErrors())
			{
				// Waiting for email letter
				wd().wait(300);
				// Check mailbox
				// Open letter content localy
				String htmlLocalPath = manager.mails().welcomeLetter().getPageContent(p_newEmail);
				manager.wd().openURL(htmlLocalPath);
				String autologinText = manager.mails().welcomeLetter().getTextAutologin();
				log().info("Autologin link => " + autologinText);
				// Get Web element of Autologin
				WebElement autologin = manager.mails().welcomeLetter().getElementAutologin();
				// Set link target to "_self"
				WebElement autologinLink = manager.wd().setLinkTarget(autologin, "_self");
				// Click at link
				manager.wd().click(autologinLink);
				page().myAccountPage().waitForPageLoad();
				// Data sync
				wd().wait(30);

				// Test
				String p_emailBackEnd = manager.backend().searchPage().getEmail();
				test().assertEquals(p_emailBackEnd, p_newEmail, "Email change");
			}

			// User update
			if (!log().isErrors())
			{
				manager.testUser().setEmail(p_newEmail);
				manager.db().updateUserData(manager.testUser());
			}
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("MyAccount check correctly change email " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
		}
	}

	/**
	 * MyAccount check correctly change password
	 */
	public void changePasswordTest()
	{
		try
		{
			// Goes to site
			wd().openPage("/account");
			// Get values
			String p_passwordValue = manager.testUser().getPassword();
			String p_passwordValueNew = p_passwordValue + "new";
			// Full up the form
			page().myAccountPage().waitForPageLoad();
			page().myAccountPage().clickBtnEditPass();
			page().myAccountPage().fillPasswordForm(p_passwordValue, p_passwordValueNew);
			page().myAccountPage().submitPassword();
			page().myAccountPage().waitForPasswordSuccess();

			// Validation
			String p_passwordBackEnd = manager.backend().searchPage().getPassword();
			// Test
			test().assertEquals(p_passwordBackEnd, p_passwordValueNew, "Password change");
			// User update
			if (!log().isErrors())
			{
				manager.testUser().setPassword(p_passwordValueNew);
				manager.db().updateUserData(manager.testUser());
			}
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("MyAccount check correctly change password " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
		}
	}

	public void passwordReminderBillingTest(String[] p_title, String p_email)
	{
		try
		{
			this.passwordReminderBillingHistory(p_email);
			wd().wait(300);
			mailTests.forgotLetterTest(testUser().getEmail());
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("recoveryPasswordTest " + p_ex.toString());
		}

		test().forErrors();
	}

	public void passwordReminderBillingHistory(String p_email)
	{
		wd().openURL(testUser().getAutologin());
		wd().openPage("/account");
		page().myAccountPage().clickBillingHistory();
		page().myAccountPage().clickDeactivateSubscribition();
		page().myAccountPage().clickRemindPassword();
		page().myAccountPage().waitForConfirmationMessage(p_email);
	}

	public void passwordReminderRemoveAccount(String p_email)
	{
		wd().openURL(testUser().getAutologin());
		wd().openPage("/account");
		wd().scrollDown();
		page().myAccountPage().clickRemoveAccount();
		page().myAccountPage().clickRemindPassword();
		page().myAccountPage().waitForConfirmationMessage(p_email);
	}

}
